package com.zynn.service.module.canal.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zynn.common.core.utils.TypeConvertUtil;
import com.zynn.feign.module.es.SyncEventFeignService;
import com.zynn.message.queue.constant.MqQueueNameConstant;
import com.zynn.message.queue.service.ScheduleMessagePostProcessor;
import com.zynn.message.queue.service.SenderService;
import com.zynn.service.module.canal.entity.Events;
import com.zynn.service.module.canal.service.EventsService;
import com.zynn.service.module.canal.service.LikeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.Objects;

/**
 *@ClassName LikeServiceImpl
 *@Description TODO
 *@Author 刘猛
 *@Date 2018/10/9 16:14
 **/

@Service
@Slf4j
public class LikeServiceImpl implements LikeService {

    @Autowired
    SyncEventFeignService syncEventFeignService;

    @Autowired
    private SenderService senderService;

    @Autowired
    EventsService eventsService;

    @Override
    public void handleInsert(Map<String, Object> columnsMap) {
        senderService.send(MqQueueNameConstant.FIND_FRIEND_LIKE, columnsMap,new ScheduleMessagePostProcessor(1000L,0L,null,null));
    }

    @JmsListener(destination = MqQueueNameConstant.FIND_FRIEND_LIKE)
    public void findDegOneFriend(Map<String, Object> columnsMap) {
        log.info("FIND_FRIEND_LIKE:{}", columnsMap);
        /**
         * 点赞人
         */
        Long likeUserID = TypeConvertUtil.objectToLong(columnsMap.get("likeUserID"));
        if(Objects.nonNull(likeUserID)){
            syncEventFeignService.initProcuctFriend(likeUserID);
        }

        /**
         * 被分享的动态id
         */
        Long likeEventID = TypeConvertUtil.objectToLong(columnsMap.get("likeEventID"));
        if(Objects.nonNull(likeEventID)){
            Events event = eventsService.selectOne(new EntityWrapper<Events>().eq("eid",likeEventID));
            if(Objects.nonNull(event)){
                /**
                 * 被分享的用户id
                 */
                Long euserId = Long.valueOf(event.getEuserid());
                if(Objects.nonNull(euserId)){
                    syncEventFeignService.initProcuctFriend(euserId);
                }
            }
        }
    }
}
